#!/bin/bash

#创建项目文件夹
mkdir -p /home/project/{src,logs,reports}

#创建日志文件
mkdir -p /home/project/logs
echo "INFO: User admin logged in at 2025-06-15" > /home/project/logs/access.log
echo "ERROR: Disk full on device /dev/sda1" >> /home/project/logs/access.log
echo "INFO: Backup completed successfully" >> /home/project/logs/access.log
echo "ERROR: Connection timeout from IP 192.168.1.5" >> /home/project/logs/access.log

#日志分析处理
grep "ERROR:" /home/project/logs/access.log >> /home/project/reports/error_report
error_count=$(grep -c "ERROR:" /home/project/logs/access.log)
echo "ERROR 出现次数: $error_count" >> /home/project/reports/error_report

#权限
chmod 700 /home/project/reports

#归档     
tar -czvf /home/project/logs.tar.gz -C /home/project logs
          
#结束    
echo "over"
(crontab -l 2>/dev/null; echo "0 0 * * * /home/project/src/analyze_logs.sh") | crontab -